/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.DonDatHangPOJO;
import POJO.NguoiDungPOJO;
import POJO.SanPhamPOJO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ultil.MySqlDataAccessHelper;

/**
 *
 * @author NGUYEN CONG BINH
 */
public class DonDatHangDAO {

    public static boolean themDonDatHang(NguoiDungPOJO kh) throws SQLException {
        boolean kq = false;
        try {
            kh.getMaNguoiDung();
            String sql = String.format("INSERT INTO dondathang( ngayDatHang, maNguoidung, maTinhTrang) VALUES (now(), %d ,1)", kh.getMaNguoiDung());
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.close();
        } catch (Exception ex) {
            System.err.println(ex);
        }
        return kq;
    }

    public static int capNhatHoaDon(NguoiDungPOJO kh, ArrayList<SanPhamPOJO> gioHang) throws SQLException {
        int flag = 0;
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            helper.setAutoCommit(false);
            //Thêm đơn đặt hàng..
            String sql = String.format(" INSERT INTO dondathang(ngayDatHang,maNguoidung,maTinhTrang) VALUES(now(),%d ,1)", kh.getMaNguoiDung());
            int j = helper.executeUpdate(sql);
            if (j == 0) {
                helper.rollback();
                return flag;
            }

            sql = " SELECT MaDonDatHang FROM dondathang WHERE maNguoidung = " + kh.getMaNguoiDung();
            ResultSet rs = helper.executeQuery(sql);
            //lấy mã hóa đơn
            int maHoaDon;
            if (rs.next()) {
                maHoaDon = rs.getInt(1);
            } else {
                helper.rollback();
                return flag;
            }
            //cập nhật chi tiết đơn đặt hàng 
            for (int i = 0; i < gioHang.size(); i++) {
                SanPhamPOJO g = gioHang.get(i);
                sql = String.format("Insert into chitietdondathang( maDonDatHang, maSanPham, soLuong, donGia) values (%d,%d,%d,%f)", maHoaDon, g.getMaSanPham(), g.getSoLuong(), g.getGiaBan());
                j = helper.executeUpdate(sql);
                if (j == 0) {
                    helper.rollback();
                    return flag;
                }
            }
            flag = 1;
            helper.commit();
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return flag;
    }

    public static int capNhatHoaDon2(NguoiDungPOJO kh, ArrayList<SanPhamPOJO> gioHang) throws SQLException {
        int flag = 0;
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            helper.setAutoCommit(false);
            //Thêm đơn đặt hàng..
            String sql = String.format(" INSERT INTO dondathang(ngayDatHang,maNguoidung,maTinhTrang) VALUES(now(),%d ,1)", kh.getMaNguoiDung());

            ResultSet rs = helper.executeInsertAndGetID(sql);
            //lấy mã hóa đơn
            int maHoaDon;
            if (rs.next()) {
                maHoaDon = rs.getInt(1);
            } else {
                helper.rollback();
                return flag;
            }
            //cập nhật chi tiết đơn đặt hàng 
            for (int i = 0; i < gioHang.size(); i++) {
                SanPhamPOJO g = gioHang.get(i);
                sql = String.format("Insert into chitietdondathang( maDonDatHang, maSanPham, soLuong, donGia) values (%d,%d,%d,%f)", maHoaDon, g.getMaSanPham(), g.getSoLuong(), g.getGiaBan());
                int j = helper.executeUpdate(sql);
                if (j == 0) {
                    helper.rollback();
                    return flag;
                }
            }
            flag = 1;
            helper.commit();
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return flag;
    }

    public static ArrayList<DonDatHangPOJO> layDanhSachDonDatHang(int maNguoiDung) {
        ArrayList<DonDatHangPOJO> donhang = new ArrayList<DonDatHangPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            String sql = "SELECT * FROM donDatHang WHERE maTinhTrang = 1 and manguoidung = " + maNguoiDung;
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                DonDatHangPOJO d = new DonDatHangPOJO();
                d.setMaDonDatHang(rs.getInt("maDonDatHang"));
                d.setMaNguoiDung(rs.getInt("maNguoiDung"));
                d.setMaTinhTrang(rs.getInt("maTinhTrang"));
                d.setNgayDatHang(rs.getTimestamp("ngayDatHang"));
                donhang.add(d);
            }
            helper.close();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return donhang;
    }

    public static ArrayList<Object> laySoLuongVaTongTien(int MaDonDatHang) {
        ArrayList<Object> kq = new ArrayList<Object>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            String sql = "SELECT SoLuong, DonGia FROM ChitietDonDatHang WHERE  maDonDatHang = " + MaDonDatHang;
            ResultSet rs = helper.executeQuery(sql);
            float tongTien = 0;
            int soLuong = 0;
            while (rs.next()) {
                tongTien += rs.getInt("soLuong") * rs.getFloat("donGia");
                soLuong += rs.getInt("soLuong");
            }
            kq.add(soLuong);
            kq.add(tongTien);
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return kq;
    }

    public static int capNhatTrangThai(int maDonDatHang, int maTinhTrang) {
        int n = 0;
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            String sql = "UPDATE dondathang SET maTinhTrang = " + maTinhTrang + " WHERE maDonDatHang = " + maDonDatHang;
            n = helper.executeUpdate(sql);
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return n;
    }
}